#ifndef COMMON_H
#define COMMON_H
#include <unistd.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <sys/types.h>
#include <signal.h>
#include <sys/wait.h>
#include <ctype.h>
#include <dirent.h>
#include <iostream>
using namespace std;

#define MAXLINE 4096
#define MAXNAME 50
#define MAXADDR 15
#define MAXDATETIME 9
#define LINES_PER_SITE 4
#define SCAN_PORT "21"
#define SCAN_USERNAME "anonymous"
#define SCAN_PASSWORD "anonymous"

#define ENV_READ_TIMEOUT "READ_TIMEOUT"
#define ENV_CONNECT_TIMEOUT "CONNECT_TIMEOUT"
#define READ_TIMEOUT_DEFAULT 5000000
#define CONNECT_TIMEOUT_DEFAULT 5000000

#define DOS_DIR "<DIR>"
#define FILENAME_SEP " ^_^ "
#define CACHE_SUFFIX ".cache"


#define FILE_CONFIG "c-sf.config"
#define CONFIG_SITES "sites"
#define CONFIG_SITES_SUBMITTED "sites_submitted"
#define CONFIG_BUGS_REPORTED "bugs_reported"
#define CONFIG_SCAN_FROM "scan_from"
#define CONFIG_SCAN_TO "scan_to"
#define CONFIG_SCAN_NUM_THREAD "scan_num_thread"
#define CONFIG_DATANAMES "dataNames"
#define CONFIG_GETNAMES_NUM_PROCESS "getNames_num_process"
#define CONFIG_DATAINDEX "dataIndex"
#define CONFIG_LISTENING_PORT "listening_port"
#define CONFIG_SCAN_INTERVAL_MINUTES "scan_interval_minutes"

#define MSG_ON
#ifdef MSG_ON
#define MSG(string,arg...) run_log("%s - %d : " string,__FILE__,__LINE__,##arg),printf("%s\n",strerror(errno));
#else
#define MSG(string,arg...)
#endif

int run_log(const char *fmt,...);

ssize_t readn(int fd,void *vptr,size_t n);
ssize_t writen(int fd,const void *vptr,size_t n);
ssize_t readline(int fd,void *vptr,size_t maxlen);

ssize_t readline2(int fd, void *vptr, size_t maxlen);
ssize_t readlinebuf(void **vptrptr);

void sig_pipe(int signo);

int connect_timeout(int sockfd,struct sockaddr *saptr, socklen_t salen);
int readable_timeout(int fd);
int readline_timeout(int fd,void *vptr,size_t maxlen);

#endif
